package com.atguigu.dao.impl;

import com.atguigu.dao.UserDao;
import com.atguigu.pojo.SysUser;
import com.atguigu.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public SysUser login(String username, String password) throws SQLException {
        String sql = "select * from sys_user where username=? and password=?";
        SysUser sysUser = queryRunner.query(sql, new BeanHandler<SysUser>(SysUser.class), username, password);
        return sysUser;
    }

    @Override
    public SysUser findUserById(Integer uid) throws SQLException {
       String  sql = "select id,username,password,nickname from sys_user where id=?";
        SysUser query = queryRunner.query(sql, new BeanHandler<>(SysUser.class), uid);
        return query;
    }

    @Override
    public void updateUserPassword(Integer uid, String newpassword) throws SQLException {
        String sql ="update sys_user set password = ? where id = ?";
        queryRunner.update(sql,newpassword,uid);
    }
}
